package com.cjh.controller;


import com.cjh.annotation.WebLog;
import com.cjh.feign.MenuFeign;
import com.cjh.pojo.Menu;
import com.cjh.pojo.Type;
import com.cjh.vo.ResultMap;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/menuApi")
public class MenuController {

    @Autowired
    private MenuFeign menuFeign;

    @ResponseBody
    @GetMapping("/menuList")
    @WebLog(description = "请求=>查询所有菜品接口")
    public ResultMap menuList(@RequestParam(value = "page", defaultValue = "1") Integer page
            , @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        PageInfo<Menu> pageInfo = menuFeign.menuList(page, limit);

        return new ResultMap<List<Menu>>(0, "所有菜品信息->每页10条数据", pageInfo.getList(), (int) pageInfo.getTotal());
    }

    @PostMapping("/insertMenu")
    @ResponseBody
    @WebLog(description = "请求=>添加菜品接口")
    public ResultMap insertMenu(Menu menu){
        boolean flag = menuFeign.insertMenu(menu);

        return new ResultMap<Boolean>(500,"添加一个菜品",flag,1);

    }

    @DeleteMapping("/deleteMenuById/{id}")
    @ResponseBody
    @WebLog(description = "请求=>删除菜品接口")
    public ResultMap deleteMenuById(@PathVariable("id") Integer id){
        boolean flag = menuFeign.deleteMenuById(id);
        return new ResultMap<String>(200,"根据id删除一个菜品","删除成功",1);
    }

    @PutMapping("/updateMenuById")
    @ResponseBody
    @WebLog(description = "请求=>修改菜品接口")
    public ResultMap updateMenuById(Menu menu){
        System.out.println("====消费者====："+menu);
        boolean flag = menuFeign.updateMenuById(menu);
        return new ResultMap<String>(200,"修改一个菜品","修改成功",1);
    }

    @ResponseBody
    @GetMapping("/typeList")
    @WebLog(description = "请求=>查询所有分类接口")
    public ResultMap typeList() {
        List<Type> types = menuFeign.typeList();

        return new ResultMap<List<Type>>(0, "所有分类信息", types, types.size());
    }
}
